<?php
/*
Plugin Name: Lates Posts Widget For Custom Posts Types
Plugin URI: http://imre.co.uk/
Description: Lates Posts Widget For Custom Posts Types
Author: Imre
Version: 1.6
Author URI: http://imre.co.uk/
*/


add_action( 'widgets_init', 'lnw_load_widgets' );

/**
 * Register our widget.
 * 'lnw_Widget' is the widget class used below.
 *
 * @since 0.1
 */
function lnw_load_widgets() {
	register_widget( 'Lnw_Widget' );
}

/**
 * Lnw Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Lnw_Widget extends WP_Widget {

	/**
	 * Widget setup.
	 */
	function Lnw_Widget() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'lnw', 'description' => __('Display Latest Entries For Custom Post Types', 'lnw') );

		/* Widget control settings. */
		$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'lnw-widget' );

		/* Create the widget. */
		$this->WP_Widget( 'lnw-widget', __('Latest Entries for Custom Post Types', 'lnw'), $widget_ops, $control_ops );
	}

	/**
	 * How to display the widget on the screen.
	 */
	function widget( $args, $instance ) {
		extract( $args );

		/* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
		$post_type = $instance['post_type'];
		$number_posts = $instance['number_posts'];

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Display the widget title if one was input (before and after defined by themes). */
		if ( $title )
			echo $before_title . $title . $after_title;

		/* Latest entries  */
		
		$args = array('post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => $number_posts);
		$posts = get_posts($args);
		echo '<ul>';
		foreach($posts as $p){
			echo '<li><a href="'.get_permalink($p->ID).'" title="">'.$p->post_title.'</a></li>';
		}
		echo '</ul>';
	
		

		/* After widget (defined by themes). */
		echo $after_widget;
	}

	/**
	 * Update the widget settings.
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['post_type'] = strip_tags( $new_instance['post_type'] );
		$instance['number_posts'] = strip_tags( $new_instance['number_posts'] );


		return $instance;
	}

	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(  'post_type' => __('post', 'lnw'), 'number_posts' => 10);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
		</p>
		
		
		<!-- Post type -->
		<p>
			<label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e('Post type:', 'hybrid'); ?></label>
			<input id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>" value="<?php echo $instance['post_type']; ?>" style="width:100%;" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'number_posts' ); ?>"><?php _e('Post number:', 'hybrid'); ?></label>
			<input id="<?php echo $this->get_field_id( 'number_posts' ); ?>" name="<?php echo $this->get_field_name( 'number_posts' ); ?>" value="<?php echo $instance['number_posts']; ?>" style="width:100%;" />
		</p>




		<!-- Show Sex? Checkbox -->


	<?php
	}
}

?>